﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace EmotionPlotter
{
    /// <summary>
    /// Interaction logic for EmotionLevels.xaml
    /// </summary>
    public partial class EmotionLevels : UserControl
    {
        //RoutedEventHandler 
        //RoutedEventArgs EmotionLevelChanged = new RoutedEventArgs();

        public static event EventHandler EmotionLevelChanged;

        public EmotionLevels()
        {
            InitializeComponent();
        }

        void AnimateControls(UIElement control, double from, double to)
        {

            if (null == control)
                return;
            DoubleAnimation newAnimation = new DoubleAnimation();
            Storyboard newStory = new Storyboard();


            newAnimation.From = from;
            newAnimation.To = to;
            newAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(Math.Abs(to - from) * 10));
            newAnimation.AccelerationRatio = 0.8;
            newStory.Completed += new EventHandler(newStory_Completed);
            newStory.Children.Add(newAnimation);

            Storyboard.SetTarget(newAnimation, control);
            Storyboard.SetTargetProperty(newAnimation, new PropertyPath(ProgressBar.ValueProperty));

            newStory.Begin();
        }

        void newStory_Completed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            // Thickness margin = progressBar_Negative.Margin;
            //margin.Left = 33 + slider1.Value * 2;
            //progressBar_Negative.Margin = margin;
            //progressBar_Negative.Width = 200-slider1.Value;
            //slider2.Value = 100 - slider1.Value;
            _NegativeEmotionValue = 100 - slider1.Value;
            _PositiveEmotionValue = slider1.Value;
            AnimateControls(progressBar_Positive, progressBar_Positive.Value, slider1.Value);
            label_PositiveNamePlusValue.Content = PositiveEmotionName + " " + _PositiveEmotionValue.ToString("000.0") + "%";
            label_NegativeNamePlusValue.Content = NegativeEmotionName + " " + _NegativeEmotionValue.ToString("000.0") + "%";


            EmotionLevelChanged.BeginInvoke(_PositiveEmotionValue, null, null, null);// += new EventHandler(EmotionLevels_EmotionLevelChanged);
        }

        //void EmotionLevels_EmotionLevelChanged(object sender, EventArgs e)
        // {
        //throw new NotImplementedException();
        // }

        private void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            // AnimateControls(progressBar_Negative, progressBar_Negative.Value, slider2.Value);
            //slider1.Value = 100 - slider2.Value;

        }


        String _EmotionName;
        public string EmotionName { get { return _EmotionName; } set { _EmotionName = value; label_Emotion.Content = _EmotionName; } }

        String _NegativeEmotionName;
        public string NegativeEmotionName { get { return _NegativeEmotionName; } set { _NegativeEmotionName = value; label_NegativeNamePlusValue.Content = _NegativeEmotionName; } }
        String _PositiveEmotionName;
        public string PositiveEmotionName { get { return _PositiveEmotionName; } set { _PositiveEmotionName = value; label_PositiveNamePlusValue.Content = _PositiveEmotionName; } }


        Double _NegativeEmotionValue;
        public Double NegativeEmotionValue { get { return _NegativeEmotionValue; } }
        Double _PositiveEmotionValue;
        public Double PositiveEmotionValue { get { return _PositiveEmotionValue; } }

        //
        Boolean _ExcludeIt = false;
        public Boolean IsExcluded
        {
            get
            {
                return _ExcludeIt;
            }

            set
            {
                checkBox_Exclude.IsChecked = value;
                _ExcludeIt = value;

            }
        }

        float _Angle;
        public float Angle
        {
            get
            {
                return _Angle;
            }

            set
            {
                _Angle = value;

            }
        }
        //System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();

        private void checkBox_Exclude_Checked(object sender, RoutedEventArgs e)
        {
            // timer.Tick += new EventHandler(timer_Tick);
            _ExcludeIt = checkBox_Exclude.IsChecked.Value;
        }

        //void timer_Tick(object sender, EventArgs e)
        //{
        //    throw new NotImplementedException();
        // }

        private void checkBox_Exclude_Unchecked(object sender, RoutedEventArgs e)
        {
            _ExcludeIt = checkBox_Exclude.IsChecked.Value;
            //this.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        String _IsNegativeEmotion;
        public String IsNegativeEmotion
        {
            get
            {
                return _IsNegativeEmotion;
            }

            set
            {
                //.Contains("Negative") ;
                if (value.Contains("Nuetral"))
                    return;
                _IsNegativeEmotion = value;

                if (value.Contains("Negative"))
                {
                    this.Background = new SolidColorBrush(Color.FromArgb(10, 255, 0, 0));
                }
                else
                {
                    this.Background = new SolidColorBrush(Color.FromArgb(10, 0, 255, 0));
                }

            }

        }
    }
}
